package FilePanel.controls;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;

import FilePanel.Control;
import FilePanel.FilePanelControl;
import FilePanel.FilePanelUI;

/**
 * @author      Erwan Osouf, Lucas Ennouchi
 * @version     1.0                                
 * @since       29/01/2012
 */

public class ControlChooser extends Control implements ActionListener{
	
	private JFileChooser chooser;
	
	public ControlChooser(JFileChooser chooser) {
		super();
		this.chooser = chooser;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		FilePanelUI view = getSuperController().getView();
		JFileChooser fileChooser = view.getFileChooser();
		view.getFileChooser().setCurrentDirectory(getSuperController().getRoot());
		int returnVal = fileChooser.showOpenDialog(view);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            getSuperController().setText(file.getPath());
            
    		String path = getSuperController().getView().getFileSelectionField().getText();
    		getSuperController().setRoot(new File(path));
        }  
	   }
}
